package com.example.module_home.vm

import android.os.Bundle
import androidx.activity.viewModels

import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.moudle_house.adapter.HouseAdapter
import com.example.house.entity.MyHouseEntity
import com.example.lib_base.path.RouterPath
import com.example.lib_common.view.view.BaseMvvmActivity
import com.example.module_home.R
import com.example.module_home.databinding.ActivityHousesBinding
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = RouterPath.PATH_HOUSE_PAGE)
class HousesActivity : BaseMvvmActivity<ActivityHousesBinding>() {

    private val houseViewModel : HouseViewModel1 by viewModels ()
    val houseAdapter = HouseAdapter()
    override fun getLayoutId(): Int {
        return R.layout.activity_houses
    }

    override fun initViews() {
        mDataBinding.rvHouse.adapter = houseAdapter
        mDataBinding.rvHouse.layoutManager = LinearLayoutManager(this)
        mDataBinding.tvA.setOnClickListener {
            houseViewModel.getListData(1)
        }
        mDataBinding.tvB.setOnClickListener {
            houseViewModel.getListData(2)
        }
        mDataBinding.tvC.setOnClickListener {
            houseViewModel.getListData(3)
        }
        mDataBinding.tvD.setOnClickListener {
            houseViewModel.getListData(4)
        }
        mDataBinding.gotoZu.setOnClickListener {
            ARouter.getInstance().build( "/module_house/HouseManngerActivity")
                .navigation()
        }
        houseViewModel.mSuccessLiveData.observe(this){
            houseAdapter.data.clear()
            val myHouseEntity = mutableListOf<MyHouseEntity>()
            val map = it.data.groupBy {
                it.area
            }
            val list = map.keys.sorted()
            var index = 1
            for (key in list){
                myHouseEntity.add(MyHouseEntity(index,key,map.get(key)!!))
                index++
            }
            houseAdapter.addData(myHouseEntity)
            houseAdapter.notifyDataSetChanged()
        }

    }

    override fun onResume() {
        super.onResume()
        houseViewModel.getListData(1)
    }

    override fun getActivityState(state: Bundle) {

    }

    override fun saveActivityState(outState: Bundle) {

    }

}